package com.zcf.service.impl;

import com.zcf.pojo.Dining;
import com.zcf.pojo.Freight;
import com.zcf.pojo.Product;
import com.zcf.pojo.Riding;
import com.zcf.pojo.Store;
import com.zcf.pojo.Subordinate;
import com.zcf.pojo.User;
import com.zcf.common.json.Body;
import com.zcf.mapper.DiningMapper;
import com.zcf.mapper.StoreMapper;
import com.zcf.mapper.UserMapper;
import com.zcf.mappers.Demo;
import com.zcf.service.StoreService;
import com.zcf.utils.Hutool;
import com.zcf.utils.JavaLarLng;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {
	@Autowired
	private StoreMapper storeMapper;
	@Autowired
	private DiningMapper diningMapper;
	@Autowired
	private UserMapper userMapper;

	public Body changeStore(String userId, String name, String range, String address, String zheng, String fan,
			String longitude, String latitude, String phone, String notice,String fl) {
		EntityWrapper<Dining> dinew = new EntityWrapper<>();
		dinew.eq("user_id", userId);
		Dining dining = new Dining();
		int count = dining.selectCount(dinew);
		if (count > 0) {
			return Body.newInstance("你已经是商家了");
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		Store store = new Store();
		store.setUserId(userId);
		EntityWrapper<Store> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		List<Store> one = storeMapper.selectList(ew);
		if (one.size() > 0) {
			return Body.newInstance(501, "正在审核中");
		}
		store.setUserId(userId);
		store.setId(Hutool.getId());
		store.setName(name);
		store.setRange(range);
		store.setPhone(phone);
		store.setAddress(address);
		if (zheng != null && zheng != "" && zheng.length() > 0) {
			store.setCardZheng(zheng);
		}
		if (fan != null && fan != "" && fan.length() > 0) {
			store.setCardFan("");//fan);
		}
		store.setStatus(0);
		store.setTime(time);

		// 根据地址获取经纬度
		Demo demo = new Demo();
		Object s =demo.gaodeditu(address);
		String appid = s.toString();
		String lon[] = appid.split(",");
		for (int i = 0; i < lon.length; i++) {
			String string = lon[i];
			if (i == 0) {
				longitude = string;
			} else {
				latitude = string;
			}
		}
		store.setLongitude(longitude);
		store.setLatitude(latitude);
		if(notice == null || notice.equals("")){
			store.setNotice(fl);
		}else {
			store.setNotice(notice);
		}
		Integer insert = storeMapper.insert(store);
		if (insert > 0) {
			return Body.newInstance(store);
		}
		return Body.newInstance(501, "上传有误");
	}

	public Body changeStatus(String id) throws Exception {
		Store store = new Store();
		store.setId(id);
		Store one = storeMapper.selectById(store);
		if (one != null && !"".equals(one)) {
			one.setStatus(2);
			Integer b = storeMapper.updateById(one);
			if (b > 0) {
				Riding riding = new Riding();
				EntityWrapper<Riding> e = new EntityWrapper<>();
				e.eq("user_id", one.getId());
				riding = riding.selectOne(e);
				if (riding != null) {
					riding.deleteById();
				}
				User user = new User();
				user.setId(one.getUserId());
				User param = user.selectById();

				Freight freight = new Freight();
				List<Freight> li = freight.selectAll();
				// 商家入驻
				Dining dining = new Dining();
				dining.setAddress(one.getAddress());

//				JavaLarLng javaLarLng = new JavaLarLng();
//				String lng = javaLarLng.getLarLng(one.getAddress());
//				String lat = javaLarLng.getLarLngs(one.getAddress());
				// 经纬度
				dining.setLongitude(one.getLongitude());
				dining.setLatitude(one.getLatitude());
				dining.setId(Hutool.getId());
				dining.setUserId(one.getUserId());
				dining.setTime(Hutool.parseDateToString());
				dining.setBox(li.get(0).getBoxMoney());
				dining.setFreight(li.get(0).getMoneys());
				dining.setDiningName(one.getName());
				dining.setDelivery(10.0);
				dining.setPhone(one.getPhone());
				dining.setGrade("5");
				dining.setLargeId(one.getNotice());
				diningMapper.insert(dining);

				// 代理抽成
				User users = new User();
				List<User> lists = users.selectAll();
				for (int i = 0; i < lists.size(); i++) {
					String address = "";
					EntityWrapper<Store> ew = new EntityWrapper<>();
					address = lists.get(i).getAddress();
					ew.like("address", address);
					Store stores = new Store();
					stores = stores.selectOne(ew);
					if (store != null) {
						Subordinate subordinate = new Subordinate();
						subordinate.setRegion(lists.get(i).getAddress());
						subordinate.setDinId(dining.getId());
						subordinate.setProportion(10);
						subordinate.setUserId(lists.get(i).getId());
						subordinate.setSubordinateId(Hutool.getId());
						subordinate.setAddtime(Hutool.parseDateToString());
						subordinate.insert();
						break;
					}
				}
//				Subordinate subordinate = new Subordinate();
//				subordinate.setUserId(param.getId());
				if (param != null && !"".equals(param)) {
					param.setStatus(2);
					userMapper.updateById(param);
					one.setDinId(dining.getId());
					return Body.newInstance(one);
				}
				return Body.newInstance(501, "请用注册过的手机号");
			}
			return Body.newInstance(501, "更改状态失败");
		}
		return Body.newInstance(501, "用户不存在");
	}

	@Override
	public Body refuseStatus(String id, String reason) {
		Store store = new Store();
		store.setId(id);
		Store one = storeMapper.selectById(store);
		if (one != null && !"".equals(one)) {
			one.setReason(reason);
			one.setStatus(2);
			Integer b = storeMapper.updateById(one);
			if (b > 0) {
//				Dining dining = new Dining();
//				dining.setAddress(one.getAddress());
//				dining.setId(one.getId());
//				dining.setPhone(one.getPhone());
//				dining.setTime(one.getTime());
//				diningMapper.insert(dining);
//				User user = new User();
//				user.setPhone(one.getPhone());
//				User param = userMapper.selectOne(user);
//				if (param != null && !"".equals(param)) {
//					param.setStatus(0);
//					userMapper.updateById(param);
				return Body.newInstance(one);
//				}
//				return Body.newInstance(501, "请用注册过的手机号");
			}
			return Body.newInstance(501, "更改状态失败");
		}
		return Body.newInstance(501, "用户不存在");
	}

}
